/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.admin.course;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author FRIENDS
 */
@WebServlet(name = "addtocoursebean", urlPatterns = {"/administrator/course/addtocoursebean"})
public class addtocoursebean extends HttpServlet {

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        AO.Course course=new AO.Course();
        
        course.setCourseid(request.getParameter("courseid"));
        course.setCourseName(request.getParameter("coursename"));
        course.setSdate(request.getParameter("startdate"));
        course.setEdate(request.getParameter("enddate"));
        course.setFee(Integer.parseInt(request.getParameter("fee")));
        course.setNoofFaculty(Integer.parseInt(request.getParameter("nooffaculty")));
        course.setNoofExams(Integer.parseInt(request.getParameter("noofexams")));
        
        AO.Announcement a=new AO.Announcement();
        a.setAnnounceid(helper.Announcement.getNewAnnouncementId());
        a.setAnnouncetype("private");
        a.setCatagory("newcourse");
        a.setTousers("all");
        a.setCourseid(course.getCourseId());
        a.setAuthor(((AO.Login)request.getSession().getAttribute("logindata")).getUserid());
        a.setSubject("introducing new course  :'"+course.getCourseId()+"'");
        a.setDescription("'"+course.getCourseId()+"'"+course.getCourseName()+System.getProperty("line.separator")+
                         "starting on: "+course.getSdate()+"  ends on: "+course.getEdate()+System.getProperty("line.separator")+
                         "with "+course.getNoofFaculty()+" lecturers , "+course.getNoofExams()+" tests "+System.getProperty("line.separator")+
                         "fee for the course is :"+course.getFee()+"Rs. only"
                         );
        
        try{DAO.CourseDAO.save(course);DAO.AnnouncementDAO.save(a);helper.Announcement.announceToUsers(a);
            response.sendRedirect("");}
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
    
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
